package com.safeluck.scannerqcr.fragment.pager;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.safeluck.scannerqcr.R;
import com.safeluck.scannerqcr.adapter.FirstPagerAdapter;
import com.safeluck.scannerqcr.fragment.BaseMainFragment;
import com.safeluck.scannerqcr.fragment.MainFragment;
import com.safeluck.scannerqcr.fragment.WechatSecondTabFragment;
import com.safeluck.scannerqcr.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * ScannerQCR
 * Created by lzw on 2017/12/4. 13:56:46
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */

public class FirstPagerFragment extends SupportFragment implements SwipeRefreshLayout.OnRefreshListener {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private FirstPagerAdapter firstPagerAdapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.other_first_pager,container,false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mRecyclerView = view.findViewById(R.id.recy_first);
        swipeRefreshLayout = view.findViewById(R.id.refresh_layout);

        swipeRefreshLayout.setOnRefreshListener(this);
        firstPagerAdapter = new FirstPagerAdapter(_mActivity);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(_mActivity));
        final int space = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,10f,getResources().getDisplayMetrics());
        mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.set(0,0,0,space);
            }
        });
        firstPagerAdapter.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(int position, View view, RecyclerView.ViewHolder viewHolder) {
                ( (MainFragment)getParentFragment().getParentFragment()).startBrotherFragment(DemoFragment.newInstance(firstPagerAdapter.getItem(position)));
            }
        });
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setAdapter(firstPagerAdapter);
        firstPagerAdapter.setDatas(initData());

    }

    private List<String> initData() {
        String[] abc = new String[]{"HelloKity","KongFuPanda","IronMan","SpiderMan","AntMan"};
        List<String> strs = new ArrayList<>();
        for (int i=0; i<15; i++){
            int index = (int) (Math.random()*abc.length);
            strs.add(abc[index]);
        }

        return strs;

    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
    }

    public static Fragment newInstance() {
        Bundle bundle = new Bundle();

        FirstPagerFragment f = new FirstPagerFragment();
        f.setArguments(bundle);
        return f;
    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
               swipeRefreshLayout.setRefreshing(false);
            }
        },3000);
    }
}
